package com.dic.activity;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

import com.dic.R;

public class MainActivity extends TabActivity {
	private TabHost mTabHost;
	private TabWidget tabWidget;
	private GestureDetector mGestureDetector;
	private int tabId = 0;

	// private int lastTabId = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);

		mTabHost = getTabHost();
		tabWidget = getTabWidget();
		mTabHost.setup();

		LayoutInflater inflater_tab1 = LayoutInflater.from(this);
		inflater_tab1.inflate(R.layout.search_main_view,
				mTabHost.getTabContentView());
		inflater_tab1.inflate(R.layout.book_view, mTabHost.getTabContentView());
		inflater_tab1
				.inflate(R.layout.about_view, mTabHost.getTabContentView());

		Intent intent = new Intent(MainActivity.this, DicActivity.class);
		mTabHost.addTab(mTabHost.newTabSpec("0")
				.setIndicator(getResources().getString(R.string.tab1), null)
				.setContent(intent));
		intent = new Intent(MainActivity.this, BookActivity.class);
		mTabHost.addTab(mTabHost.newTabSpec("1")
				.setIndicator(getResources().getString(R.string.tab2), null)
				.setContent(intent));
		intent = new Intent(MainActivity.this, AboutActivity.class);
		mTabHost.addTab(mTabHost.newTabSpec("2")
				.setIndicator(getResources().getString(R.string.tab3), null)
				.setContent(intent));
		overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
		mTabHost.setCurrentTab(tabId);
		setTabBackground();
		mTabHost.setLongClickable(true);
		mTabHost.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {

				return mGestureDetector.onTouchEvent(event);
			}
		});

		mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {

			@Override
			public void onTabChanged(String Id) {
				tabId = mTabHost.getCurrentTab();
				setTabBackground();
			}
		});

	}

	public void setTabBackground() {
		for (int i = 0; i < tabWidget.getChildCount(); i++) {
			RelativeLayout rl = (RelativeLayout) tabWidget.getChildAt(i);
			TextView tv = (TextView) rl.getChildAt(1);
			tv.getLayoutParams().height = rl.getLayoutParams().height;
			tv.setTextSize(34);
			tv.setGravity(Gravity.CENTER);
			if (i == tabId) {
				rl.setBackgroundResource(R.drawable.tab_select);
				tv.setTextColor(Color.CYAN);
			} else {

				rl.setBackgroundResource(R.drawable.tab_nomal);
				tabWidget.getChildAt(i).setClickable(true);
				tv.setTextColor(Color.BLACK);
			}
		}
	}
}
